<template>
  <div class="user">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input
          v-model="searchParams.userId"
          placeholder="请输入用户id"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-select
          v-model="searchParams.status"
          placeholder="请选择提现状态"
          clearable
          :style="{ width: '150px' }"
          size="mini"
        >
          <el-option
            v-for="item in statusArr"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-date-picker
          v-model="searchParams.startTime"
          type="datetime"
          placeholder="选择开始时间"
          size="mini"
        ></el-date-picker>
        <el-date-picker
          v-model="searchParams.endTime"
          type="datetime"
          placeholder="选择结束时间"
          size="mini"
        ></el-date-picker>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('userWithdraws/index')"
          >搜索</el-button
        >
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          @click="reset"
          v-if="has('userWithdraws/index')"
          >重置</el-button
        >
      </el-row>
      <el-table
        :data="data.data"
        v-loading="table_loading"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="ID"
          align="center"
        ></el-table-column>
        <el-table-column label="用户信息" align="center" width="200px">
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-image
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
                fit="contain"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px',
                  alignItems: 'center'
                }"
              ></el-image>
              <div
                style="display: flex;flex-direction: column;justify-content: space-between;"
              >
                <span>{{ scope.row.nickname }}</span>
                <span>ID:{{ scope.row.user_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="提现状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 1" style="color:red">提现中</span>
            <span v-if="scope.row.status === 2" style="color:green"
              >提现通过</span
            >
            <span v-if="scope.row.status === 3" style="color:red"
              >提现失败</span
            >
          </template>
        </el-table-column>

        <el-table-column
          label="提现金额"
          prop="amount"
          align="center"
        ></el-table-column>
        <el-table-column
          label="提现后金额"
          prop="after_amount"
          align="center"
        ></el-table-column>
        <el-table-column label="收款码" align="center" width="200px">
          <template slot-scope="scope">
            <el-image
              :src="scope.row.image"
              :preview-src-list="[scope.row.image]"
              fit="contain"
              :style="{
                width: '50px',
                height: '50px',
                borderRadius: '5px',
                marginRight: '10px',
                alignItems: 'center'
              }"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="提现时间"
          align="center"
        ></el-table-column>
        <el-table-column prop="admin_username" label="操作人" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.admin_username">{{
              scope.row.admin_username
            }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="action_at" label="操作时间" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.admin_username">{{
              scope.row.action_at
            }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="submit(scope.row.id, 2)"
              v-if="has('userWithdraws/update') && scope.row.status == 1"
              >通过</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="submit(scope.row.id, 3)"
              v-if="has('userWithdraws/update') && scope.row.status == 1"
              >拒绝</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>

    <el-dialog title="提现详情" :visible.sync="detailStatus" @close="init">
      <el-form :model="editParams" label-width="120px">
        <el-form-item label="用户昵称">
          <el-input v-model="editParams.user.nickname" readonly></el-input>
        </el-form-item>
        <el-form-item label="提现状态">
          <el-select
            v-model="editParams.status"
            v-if="cash_status == 2"
            disabled
          >
            <el-option
              v-for="item in statusArr"
              :value="item.id"
              :key="item.id"
              :label="item.title"
              readonly
            ></el-option>
          </el-select>

          <el-select v-model="editParams.status" v-else>
            <el-option
              v-for="item in statusArr"
              :value="item.id"
              :key="item.id"
              :label="item.title"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提现金额">
          <el-input v-model="editParams.amount" readonly></el-input>
        </el-form-item>
        <el-form-item label="不通过原因" v-if="editParams.status === 3">
          <el-input
            v-model="editParams.remark"
            readonly
            v-if="cash_status === 3"
          ></el-input>
          <el-input
            v-model="editParams.remark"
            v-if="editParams.status === 3"
          ></el-input>
        </el-form-item>
        <el-form-item label="提现时间">
          <el-input v-model="editParams.created_at" readonly></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submit"
          v-if="
            has('userWithdraws/edit') &&
              cash_status !== 2 &&
              editParams.status !== 1
          "
          >确定提现</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from "../../api/api.js";
export default {
  name: "cash",
  data() {
    return {
      status: "index", // 页面控制
      cash_status: false, // 提现状态
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      statusArr: [
        { id: 1, title: "提现中" },
        { id: 2, title: "提现通过" },
        { id: 3, title: "提现失败" }
      ],
      detailStatus: false,
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        // 搜索参数
        page: 1,
        limit: 10,
        userId: "",
        status: ""
      },
      editParams: {
        user: {
          nickname: ""
        },
        status: 1,
        created_at: "",
        amount: "",
        remark: ""
      }
    };
  },
  created() {
    this.getData();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },

    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.userWithdraws
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code === 200) {
            this.table_loading = false;
            this.data = res.data.data;
            this.cash_status = res.data.data.status;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    // 初始化
    init() {
      this.searchParams.userId = "";
      this.searchParams.status = "";
      this.editParams = {
        user: {
          nickname: ""
        },
        status: 1,
        created_at: "",
        amount: "",
        remark: ""
      };
      this.detailStatus = false;
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    // 添加页面
    detail(id) {
      this.loading = true;
      api.userWithdraws
        .show(id, this.searchParams)
        .then(res => {
          if (res.data.code === 200) {
            this.loading = false;
            this.detailStatus = true;
            this.editParams = res.data.data;
            this.cash_status = this.editParams.status;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 确定提现
    submit(id, status) {
      this.$confirm(`是否确认?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)"
          });
          api.userWithdraws
            .edit(id, {
              status: status
            })
            .then(res => {
              if (res.data.code === 200) {
                this.$message.success(res.data.msg);
                this.init();
                loading.close();
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    }
  }
};
</script>

<style scoped lang="less">
.user {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
